package cn.jcet.module.bus.controller;


import cn.jcet.common.utils.AppFileUtils;
import cn.jcet.common.utils.Constast;
import cn.jcet.common.utils.DataGridView;
import cn.jcet.common.utils.ResultObj;
import cn.jcet.module.bus.domain.Goods;
import cn.jcet.module.bus.domain.Provider;
import cn.jcet.module.bus.service.IGoodsService;
import cn.jcet.module.bus.service.IProviderService;
import cn.jcet.module.bus.vo.GoodsVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 鲜磊
 * @since 2019-12-24
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private IProviderService providerService;


    /**
     * 加载全部
     * @param goodsVo
     * @return
     */
    @GetMapping("loadAllGoods")
    public DataGridView loadAllGoods(GoodsVo goodsVo){
        IPage<Goods> page = new Page<>(goodsVo.getPage(),goodsVo.getLimit());

        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(goodsVo.getProviderid()!=null && goodsVo.getProviderid()!=0,"providerid",goodsVo.getProviderid());
        queryWrapper.like(StringUtils.isNotBlank(goodsVo.getGoodsname()),"goodsname",goodsVo.getGoodsname());
        queryWrapper.like(StringUtils.isNotBlank(goodsVo.getProductcode()),"productcode",goodsVo.getProductcode());
        queryWrapper.like(StringUtils.isNotBlank(goodsVo.getPromitcode()),"promitcode",goodsVo.getPromitcode());
        queryWrapper.like(StringUtils.isNotBlank(goodsVo.getDescription()),"description",goodsVo.getDescription());
        queryWrapper.like(StringUtils.isNotBlank(goodsVo.getSize()),"size",goodsVo.getSize());
        this.goodsService.page(page,queryWrapper);
        List<Goods> records = page.getRecords();
        for (Goods goods:records){
            Provider provider = this.providerService.getById(goods.getProviderid());
            if(null != provider){
                goods.setProvidername(provider.getProvidername());
            }
        }
        return new DataGridView(page.getTotal(),records);
    }

    /**
     * 添加
     * @param goodsVo
     * @return
     */
    @PostMapping("addGoods")
    public ResultObj addGoods(GoodsVo goodsVo) {
        try {
            if(goodsVo.getGoodsimg()!=null&&goodsVo.getGoodsimg().endsWith("_temp")) {
                String newName=AppFileUtils.renameFile(goodsVo.getGoodsimg());
                goodsVo.setGoodsimg(newName);
            }
            this.goodsService.save(goodsVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 更新
     * @param goodsVo
     * @return
     */
    @PostMapping("updateGoods")
    public ResultObj updateGoods(GoodsVo goodsVo) {
        try {
            if (goodsVo.getGoodsimg() != null && goodsVo.getGoodsimg().equals(Constast.IMAGES_DEFAULTGOODSIMG_PNG)) {
                if (goodsVo.getGoodsimg().endsWith("_temp")) {
                    String newName = AppFileUtils.renameFile(goodsVo.getGoodsimg());
                    goodsVo.setGoodsimg(newName);
                    //删除原先的图片
                    String oldPath = this.goodsService.getById(goodsVo.getId()).getGoodsimg();
                    if (!oldPath.equals(Constast.IMAGES_DEFAULTGOODSIMG_PNG)) {
                        AppFileUtils.removeFileByPath(oldPath);
                    }
                }
            }
            this.goodsService.updateById(goodsVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除
     * @param goodsimg,id
     * @return
     */
    @PostMapping("deleteGoods")
    public ResultObj deleteGoods(String goodsimg,Integer id){
        try {
            //删除源文件
            AppFileUtils.removeFileByPath(goodsimg);
            this.goodsService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 加载商品管理
     * @return
     */
    @GetMapping("loadAllGoodsForSelect")
    public DataGridView loadAllGoodsForSelect(){
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("available",Constast.AVAILABLE_TRUE);
        List<Goods> list = this.goodsService.list(queryWrapper);
        for (Goods goods :list){
            Provider provider = this.providerService.getById(goods.getProviderid());
            if(null != provider){
                goods.setProvidername(provider.getProvidername());
            }
        }
        return new DataGridView(list);
    }

    /**
     * 根据供应商id查询商品信息
     * @param providerId
     * @return
     */
    @GetMapping("loadGoodsByProviderId")
    public DataGridView loadGoodsByProviderId(Integer providerId){
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("available",Constast.AVAILABLE_TRUE);
        queryWrapper.eq(providerId!=null,"providerid",providerId);
        List<Goods> list = this.goodsService.list(queryWrapper);
        for (Goods goods : list){
            Provider provider = this.providerService.getById(goods.getProviderid());
            if(null != provider){
                goods.setProvidername(provider.getProvidername());
            }
        }
        return new DataGridView(list);
    }
}
